using UnityEngine;
using QFramework;
using UnityEngine.UI;

namespace ProjectSurvivor
{
    public partial class FloatingTextController : ViewController
    {
        public static FloatingTextController mDefault;
        private void Awake()
        {
            mDefault = this;
        }
        private void OnDestroy()
        {
            mDefault = null;
        }
        void Start()
        {
            // Code Here
            FloatingText.Hide();
        }
        public static void Play(Vector2 position, string content,bool cirtical = false)
        {
            mDefault.FloatingText.InstantiateWithParent(mDefault.transform)
                .Position(position.x, position.y)
                .Self(f =>
            {
                var positionY = position.y;
                var textTrans = f.transform.Find("Text");
                var textComp = textTrans.GetComponent<Text>();
                textComp.text = content;

                if(cirtical)
                {
                    textComp.color = Color.red;
                }
                ActionKit.Sequence().Lerp(0, 0.5f, 0.5f, (p) =>
                {
                    f.PositionY(positionY + p * 0.5f);
                    textComp.LocalScaleX(Mathf.Clamp01(p * 4));
                    textComp.LocalScaleY(Mathf.Clamp01(p * 4));
                }).Delay(0.5f)
                .Lerp(1.0f, 0, 0.3f, (p) =>
                {
                    textComp.ColorAlpha(p);
                }, () =>
                {
                    textTrans.DestroyGameObjGracefully();
                }).Start(textComp);
            }).Show();
        }
    }
}
